#!/usr/bin/perl
use strict;

my ($ITER, $RANGE) = (200, 50);

sub compare_arrays(\@\@$)
{
	my ($arr1,  $arr2,  $name) = @_;
	if (scalar @$arr1 != scalar @$arr2)
	{
		die "FAILED $name ". scalar @$arr1. "!=". scalar @$arr2. "\n";;
	}
	for my $i (0.. (@$arr1 - 1) )
	{
		if ($arr1->[$i] ne $arr2->[$i])
		{
			print STDERR "$arr1->[$i] ne $arr2->[$i]\n";
			die "FAILED $name" ;
		}
	}
}


my %seq_pairs;
print STDERR "\tGenerating test files\n";
{
	open RESULTS, ">seq_pairs.test";
	#print  RESULTS "seq1\tseq2\tsfsadf\n----\t----\t----\n";
	my $cnt_results = 0;
	print STDERR "\t[";
	for (0..$ITER)
	{
		
		print STDERR "." if ($_ % ($ITER / 50) == 0);
		++$cnt_results;
		my $id1 =  int(rand($RANGE));
		my $id2 =  int(rand($RANGE));

		my $ids = ("id_$id1" lt "id_$id2") ? "id_$id1\tid_$id2": "id_$id2\tid_$id1";

		my $line = "id_$id1\tid_$id2\tresult $cnt_results";
		print RESULTS $line, "\n";
		if (!exists $seq_pairs{$ids})
		{
			$seq_pairs{$ids} = $line;
		}
	}
	print STDERR "]\n";
}
my @orig_arr = sort values %seq_pairs;






print STDERR "\tRunning seq_pairs_remove_duplicate_results\n\n";
{
	system("seq_pairs_remove_duplicate_results -v -i seq_pairs.test  > seq_pairs.unique.test");
	print STDERR "seq_pairs_remove_duplicate_results -v -i seq_pairs.test  > seq_pairs.unique.test";
}
print STDERR "\n\n";










print STDERR "\tComparing results...\n";
{
	open (PAIRS, "seq_pairs.unique.test") or die;
	my @new_arr = (<PAIRS>);
	chomp @new_arr;
	@new_arr = sort @new_arr;

	compare_arrays(@new_arr,  @orig_arr, "unique results\n");
}






print STDERR "\tCleaning up...\n";
unlink (glob("*.test"));
print STDERR "\n\tSUCCESS!!!\n\n";

